import Base64 from "./base64.js"
// import store from "../store/index.js"
import settings from "@/settings"

export default class Parse {

  // private property  

  static getInstance() {
    if (!this.instance) {
      this.instance = new Parse();
    }
    return this.instance
  }

  info = {}
  // public method for decoding  
  _processor(res) {
    const base64 = Base64.getInstance();
    let data = res.replace('/','');
    let issueId
    let labelId
    
    // let info = {
    //   enterpriseId: undefined,
    //   labelId: undefined,
    //   issueId: undefined
    // }

    const idArr = data.match(/^\d{1,}/g)
    console.log('idArr',idArr)
    if (idArr === null) {
      throw new Error("网址是个啥玩意？！")
    }
    // 处理后的data
    data = data.replace(/^\d{1,}\//g, '')
    console.log('data',data);
    if(data == ''){  //空串
      throw new Error('解析字符是个空串')
    }
    // 解压后的data
    data = base64.decode(data)
    console.log('data',data);

    // 领取方式id
    if (data.includes('i')) {
      const issueIdStrArr = data.match(/i\d{1,}/g)
      console.log('issueIdStrArr',issueIdStrArr)
      if (issueIdStrArr === null) {
        return
      }
      const issueIdArr = issueIdStrArr[0].match(/\d{1,}/g)
      console.log('issueIdArr',issueIdArr);
      if (issueIdArr === null) {
        return
      }
      issueId = issueIdArr[0]
      console.log('issueId',issueId)
      this.info.issueId = issueId
    }

    // 标签id
    if (data.includes('l')) {
      const labelIdStrArr = data.match(/l\d{1,}/g)
      if (labelIdStrArr === null) {
        return
      }
      const labelIdArr = labelIdStrArr[0].match(/\d{1,}/g)
      if (labelIdArr === null) {
        return
      }
      labelId = labelIdArr[0]
      console.log('labelId',labelId);
      this.info.labelId = labelId
    }


    let flag = false
    const infoValues = Object.values(this.info)
    for (const value of infoValues) {
      if(!!value){
        flag = true
        break
      }
    }
    if(flag){
      return this.info
    }else{
      throw new Error("啥也么解析到~")
    }
  }

  async parseCode(res) {
    try {
      this._processor(res)
      return Promise.resolve(this.info)
    } catch (e) {
      console.log(e);
      return Promise.reject("亲，解析错误~")
    }
  }

  // async _scanCode() {
  //   try {
  //     const scanCodePro = await uni.scanCode({
  //       onlyFromCamera: true
  //     })
  //     if (scanCodePro[0] != null && scanCodePro[0].errMsg == "scanCode:fail") {
  //       throw new Error(1)
  //     }
  //     if (scanCodePro[0] != null) {
  //       throw new Error(2)
  //     }
  //     return this.parseCode(scanCodePro[1].result)
  //   } catch (e) {
  //     if(e.message == 1){
  //       return Promise.reject("亲，请扫油站提供的码呦~")
  //     }else{
  //       return Promise.reject("亲，请扫二维码呀~")
  //     }
  //     // const showModalPro = await uni.showModal({
  //     //   title: '( •̀ ω •́ )✧',
  //     //   showCancel: false,
  //     //   content: '请扫描二维码呦',
  //     //   confirmText: '重新扫码',
  //     // })
  //     // return this.scanCode();
  //   }
  // }

  // async scanCode(showModalFlag = true, moduleConfig = {}) { //msg 消息 
  //   let showModalPro = [
  //     null,
  //     {
  //       cancel: false,
  //       confirm: true
  //     }
  //   ]
  //   if (showModalFlag) {
  //     showModalPro = await uni.showModal({
  //       title: moduleConfig.title || '(～￣▽￣)～',
  //       showCancel: moduleConfig.showCancel,
  //       content: moduleConfig.content || '亲，请扫码使用~',
  //       cancelText: moduleConfig.cancelText || '返回',
  //       confirmText: moduleConfig.confirmText || '立刻扫码',
  //     })
  //   }

  //   if (showModalPro[1].cancel) {
  //     uni.navigateBack({
  //       delta: 1
  //     })
  //     return Promise.reject("返回")
  //   } else if (showModalPro[1].confirm) {
  //     return this._scanCode().catch((errMsg) => {
  //       console.log(errMsg)
  //       return this.scanCode(true, {
  //         content: errMsg
  //       })
  //     });
  //   }
  // }
}
